﻿/*-----------------------------------------------------------------------------------------
   Author               Date            Description
   ----------------------------------------------------------------------------------------
   Steven Applegate     01/30/2011      Initial creation
 ----------------------------------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace NCPA.Common
{
    public static class HtmlExtensions
    {
        /// <summary>
        /// Author: Steven Applegate (01/30/2011)
        /// Description: Creates an action link for an action and controller
        /// </summary>
        public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string action, string controllerName, NameValueCollection queryString, string className)
        {
            var newRoute = new RouteValueDictionary();

            IDictionary<string, object> htmlAttributes = new Dictionary<string, object> {{"class", className}};

            newRoute = htmlHelper.ViewContext.HttpContext.Request.QueryString.ToRouteDic(newRoute);

            return HtmlHelper.GenerateLink(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection,
                linkText, null, action, null, newRoute, htmlAttributes).ToMvcHtml();
        }



        public static MvcHtmlString ActionLinkWithImage(this HtmlHelper html, string imgSrc, string actionName, string controllerName, string @class)
        {
            var urlHelper = new UrlHelper(html.ViewContext.RequestContext);

            string imgUrl = urlHelper.Content(imgSrc);
            TagBuilder imgTagBuilder = new TagBuilder("img");
            imgTagBuilder.MergeAttribute("src", imgUrl);
            string img = imgTagBuilder.ToString(TagRenderMode.SelfClosing);

            string url = urlHelper.Action(actionName);

            TagBuilder tagBuilder = new TagBuilder("a")
            {
                InnerHtml = img
            };
            tagBuilder.MergeAttribute("href", url);
            tagBuilder.MergeAttribute("class", @class);

            return tagBuilder.ToString(TagRenderMode.Normal).ToMvcHtml();
        }



        /// <summary>
        /// Author: Steven Applegate (01/30/2011)
        /// Description: Creates an action query link
        /// </summary>
        public static MvcHtmlString ActionQueryLink(this HtmlHelper htmlHelper, string linkText, string action, object routeValues)
        {
            var newRoute = routeValues == null
                ? htmlHelper.ViewContext.RouteData.Values
                : new RouteValueDictionary(routeValues);

            newRoute = htmlHelper.ViewContext.HttpContext.Request.QueryString.ToRouteDic(newRoute);

            return HtmlHelper.GenerateLink(htmlHelper.ViewContext.RequestContext,
                htmlHelper.RouteCollection, linkText, null,
                action, null, newRoute, null).ToMvcHtml();
        }



        public static MvcHtmlString MenuLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
        {
            string currentAction = htmlHelper.ViewContext.RouteData.GetRequiredString("action");
            string currentController = htmlHelper.ViewContext.RouteData.GetRequiredString("controller");

            if (actionName.ToLower() == currentAction.ToLower() && controllerName.ToLower() == currentController.ToLower())
            {
                return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues,
                    new
                    {
                        @class = "current"
                    });
            }

            return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
        }



        /// <summary>
        /// Author: Steven Applegate (01/30/2011)
        /// Description: Returns an HTML string given a string
        /// </summary>
        public static MvcHtmlString ToMvcHtml(this string content)
        {
            return MvcHtmlString.Create(content);
        }



        /// <summary>
        /// Author: Steven Applegate (01/30/2011)
        /// Description: Returns a route value dictionary given a name/value collecdtion
        /// </summary>
        public static RouteValueDictionary ToRouteDic(this NameValueCollection collection)
        {
            return collection.ToRouteDic(new RouteValueDictionary());
        }



        /// <summary>
        /// Author: Steven Applegate (01/30/2011)
        /// Description: Returns a route value dictionary given a name/value collecdtion
        /// </summary>
        public static RouteValueDictionary ToRouteDic(this NameValueCollection collection, RouteValueDictionary routeDic)
        {
            foreach (string key in collection.Keys)
            {
                if (!routeDic.ContainsKey(key))
                    routeDic.Add(key, collection[key]);
            }
            return routeDic;
        }



        /// <summary>
        /// Author: Steven Applegate (01/30/2011)
        /// Description: Returns HTML for a content region
        /// </summary>
        public static MvcHtmlString GetContentRegionContent(this HtmlHelper htmlHelper, string name)
        {
            return htmlHelper.Action("GetContent", "ContentRegions", new { name });
        }
    }
}